package jp.team.nature.reizoukoapp.adapter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import jp.team.nature.reizoukoapp.R;
import jp.team.nature.reizoukoapp.db.ReizoukoItem;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class ReizoukoItemAdapter extends ArrayAdapter<ReizoukoItem> {
	private LayoutInflater mInflater;
	private TextView mName;
	private TextView mQuantity;
	private TextView mExpDate;
	private TextView mUnit;
	private Button mButton;

	public ReizoukoItemAdapter(Context context, List<ReizoukoItem> objects) {
		super(context, 0, objects);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public View getView(final int position, View convertView, final ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.row_kakunin, null);
		}
		convertView.setBackgroundColor(Color.parseColor("#e0ffff"));
		final ReizoukoItem item = this.getItem(position);
		mName = (TextView) convertView.findViewById(R.id.nameText);
		mQuantity = (TextView) convertView.findViewById(R.id.quantityText);
		mExpDate = (TextView) convertView.findViewById(R.id.expDateText);
		mUnit = (TextView) convertView.findViewById(R.id.unitText);
		mButton = (Button) convertView.findViewById(R.id.henkouButton);
		mButton.setVisibility(View.VISIBLE);
		mExpDate.setTextColor(Color.BLACK);
		if (isEnabled(position)) {
			// 項目名セット
			mName.setText(item.getName());
			mQuantity.setText(item.getQuantity().toString());
			mExpDate.setText(item.getExpDate());
			mUnit.setText(item.getUnit());

			// 賞味期限チェック
			if (! item.getExpDate().equals("")) {
				int checkExpDate = checkDate(item.getExpDate());
				if (checkExpDate == 1) {
					mExpDate.setTextColor(Color.parseColor("#FFD700"));
				} else if (checkExpDate == 2) {
					mExpDate.setTextColor(Color.RED);
				}
			}

			// ボタンセット
			mButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					((ListView) parent).performItemClick(v, position, item.getId());
				}
			});
		} else {
			convertView.setBackgroundColor(Color.CYAN);
			mName.setText("商品名");
			mQuantity.setText("量");
			mUnit.setText("単位");
			mExpDate.setText("賞味期限");
			mExpDate.setTextColor(Color.BLACK);
			mButton.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	@Override
	public boolean isEnabled(int position) {
		return getItem(position).isEnabled();

	}


	@SuppressLint("SimpleDateFormat")
	private int checkDate(String arg) {
		int result = 0;
		Date today = new Date();
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		try {
			Date expDate = df.parse(arg);

			int diff = diffDay(today, expDate);
			if (diff < 0) {
				result = 2;
			} else if (diff < 3) {
				result = 1;
			}
		} catch (ParseException e) {
			// エラー処理はとりあえずしない
			e.printStackTrace();
		}
		return result;
	}

	private int diffDay(Date date1, Date date2) {
		long datetime1 = date1.getTime();
		long datetime2 = date2.getTime();

		long oneday = 1000 * 60 * 60 * 24;
		long diffdays = (datetime2 - datetime1) / oneday;

		return (int) diffdays;
	}
}